﻿namespace iFinance.Modules.Outgoings.ViewModels
{
    using System.Linq;
    using System.Windows.Input;
    using iFinance.IDAL;
    using iFinance.Infrastructure;
    using iFinance.Infrastructure.Models;
    using iFinance.Infrastructure.Services;
    using iFinance.Modules.Outgoings.Events;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Prism.ViewModel;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// 支出界面的ViewModel
    /// </summary>
    public class OutgoingsViewModel : NotificationObject
    {
        #region Ctor

        public OutgoingsViewModel()
        {
            AddOutgoingsCommand = new DelegateCommand(ShowAddOutgoingsView);
            DeleteOutgoingsCommand = new DelegateCommand(DeleteOutgoings);
            ModifyOutgoingsCommand = new DelegateCommand<OutgoingsItemViewModel>(ShowModifyOutgoingsView, CanModifyOutgoings);

            var dao = UnityHelper.Container.Resolve<IOutgoingsDAO>();
            if (dao != null)
            {
                var outgoings = dao.QueryAllOutgoings();
                if (outgoings != null)
                {
                    foreach (var item in outgoings)
                    {
                        List.Add(new OutgoingsItemViewModel(item));
                    }
                }
            }

            //订阅支出项添加完毕事件
            UnityHelper.Container.Resolve<IEventAggregator>().GetEvent<OutgoingsAddedEvent>().Subscribe(AddOutgoings);
        }

        #endregion

        #region 属性

        private OutgoingsListViewModel list = new OutgoingsListViewModel();
        /// <summary>
        /// 指出列表
        /// </summary>
        public OutgoingsListViewModel List
        {
            get { return this.list; }
            set
            {
                if (this.list != value)
                {
                    this.list = value;
                    RaisePropertyChanged(() => this.List);
                }
            }
        }

        #endregion

        #region 命令

        #region AddOutgoingsCommand

        private ICommand addOutgoingsCommand = null;

        /// <summary>
        /// 添加支出项命令
        /// </summary>
        public ICommand AddOutgoingsCommand
        {
            get { return this.deleteOutgoingsCommand; }
            set
            {
                if (this.deleteOutgoingsCommand != value)
                {
                    this.deleteOutgoingsCommand = value;
                    RaisePropertyChanged(() => this.AddOutgoingsCommand);
                }
            }
        }

        /// <summary>
        /// 显示添加支出View
        /// </summary>
        public void ShowAddOutgoingsView()
        {
            var service = UnityHelper.Container.Resolve<IChildWindowService>();
            if (service != null)
            {
                OutgoingsMaintainViewModel viewModel = new OutgoingsMaintainViewModel { Mode = ManintainMode.New };
                service.ResolveAndShow(viewModel);
            }
        }

        #endregion

        #region DeleteOutgoingsCommand

        private ICommand deleteOutgoingsCommand = null;

        /// <summary>
        /// 删除支出项命令
        /// </summary>
        public ICommand DeleteOutgoingsCommand
        {
            get { return this.deleteOutgoingsCommand; }
            set
            {
                if (this.deleteOutgoingsCommand != value)
                {
                    this.deleteOutgoingsCommand = value;
                    RaisePropertyChanged(() => this.DeleteOutgoingsCommand);
                }
            }
        }

        /// <summary>
        /// 删除支出
        /// </summary>
        public void DeleteOutgoings()
        {
            var items = (from OutgoingsItemViewModel item in List where item.IsChecked select item).ToList();
            if (items != null)
            {
                var dao = UnityHelper.Container.Resolve<IOutgoingsDAO>();
                if (dao != null)
                {
                    foreach (var item in items)
                    {
                        var isSuccess = dao.DeleteOutgoings(item.RealData.ID);
                        //删除成功后从集合中删除
                        if (isSuccess)
                        {
                            List.Remove(item);
                        }
                    }
                }
            }
        }

        #endregion

        #region ModifyOutgoingsCommand

        private ICommand modifyOutgoingsCommand = null;

        /// <summary>
        /// 修改支出项命令
        /// </summary>
        public ICommand ModifyOutgoingsCommand
        {
            get { return this.modifyOutgoingsCommand; }
            set
            {
                if (this.modifyOutgoingsCommand != value)
                {
                    this.modifyOutgoingsCommand = value;
                    RaisePropertyChanged(() => this.ModifyOutgoingsCommand);
                }
            }
        }

        /// <summary>
        /// 能否修改支出
        /// </summary>
        /// <param name="outgoingsItem"></param>
        /// <returns></returns>
        public bool CanModifyOutgoings(OutgoingsItemViewModel outgoingsItem)
        {
            return outgoingsItem != null;
        }

        /// <summary>
        /// 修改支出
        /// </summary>
        public void ShowModifyOutgoingsView(OutgoingsItemViewModel outgoingsItem)
        {
            if (outgoingsItem != null)
            {
                var service = UnityHelper.Container.Resolve<IChildWindowService>();
                if (service != null)
                {
                    OutgoingsMaintainViewModel viewModel = new OutgoingsMaintainViewModel(outgoingsItem) { Mode = ManintainMode.Modify };
                    service.ResolveAndShow(viewModel);
                }
            }
        }

        #endregion

        #endregion

        #region 方法

        /// <summary>
        /// 添加支出项
        /// </summary>
        /// <param name="outgoingsItem"></param>
        public void AddOutgoings(OutgoingsItemViewModel outgoingsItem)
        {
            if (outgoingsItem != null && !List.Contains(outgoingsItem))
            {
                List.Add(outgoingsItem);
            }
        }

        #endregion
    }
}
